package com.example.club.clubmanage.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

import com.example.club.clubmanage.Constant;
import com.example.club.clubmanage.MyApplication;
import com.example.club.clubmanage.model.UserDto;

import org.springframework.util.support.Base64;

import java.io.IOException;
import java.io.Serializable;
import java.util.Set;

/**
 * Created by Caolibao on 2015/9/17.
 */
public class SharedPreferencesUtil {

    /**
     * 写数据
     *
     * @param spName cn.com.quickpark.ssp.Constant.SharedPreferences下定义的sp
     */
    public static boolean write(String spName, String key, Object value) {
        Context context = MyApplication.application;
        SharedPreferences preferences = context
            .getSharedPreferences(spName, Context.MODE_PRIVATE);//创建仅允许本应用使用的SharedPreferences
        Editor editor = preferences.edit();
        if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (int) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (float) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (boolean) value);
        } else if (value instanceof Set) {
            editor.putStringSet(key, (Set<String>) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (long) value);
        } else { //写对象
            try {
                Serializable serialObi = (Serializable) value;
                String base64Str = Base64
                    .encodeObject(serialObi);//通过Spring 4 android将这个value对象转为字符串
                editor.putString(key, base64Str);
            } catch (IOException ioe) {

                return false;
            } catch (ClassCastException caste) {

                return false;
            }
        }
        return editor.commit();
    }

    /**
     * 读数据
     *
     * @param spName cn.com.quickpark.ssp.Constant.SharedPreferences下定义的sp
     */
    public static <T> T read(String spName, String key, Class<T> type, T defaultVal) {
        Context context = MyApplication.application;
        SharedPreferences preferences = context
            .getSharedPreferences(spName, Context.MODE_PRIVATE);//创建仅允许本应用使用的SharedPreferences
        if (String.class.equals(type)) {
            return (T) preferences
                .getString(key, defaultVal != null ? ((String) defaultVal) : null);
        } else if (Integer.class.equals(type)) {
            return (T) new Integer(
                preferences.getInt(key, defaultVal != null ? ((Integer) defaultVal) : null));
        } else if (Float.class.equals(type)) {
            return (T) new Float(
                preferences.getFloat(key, defaultVal != null ? ((Float) defaultVal) : null));
        } else if (Boolean.class.equals(type)) {
            return (T) new Boolean(
                preferences.getBoolean(key, defaultVal != null ? ((Boolean) defaultVal) : null));
        } else if (Set.class.equals(type)) {
            return (T) preferences
                .getStringSet(key, defaultVal != null ? ((Set) defaultVal) : null);
        } else if (Long.class.equals(type)) {
            return (T) new Long(
                preferences.getLong(key, defaultVal != null ? ((Long) defaultVal) : null));
        } else { //读对象
            try {
                String objStr = preferences.getString(key, "");
                if (TextUtils.isEmpty(objStr)) {
                    return null;
                }
                return (T) Base64.decodeToObject(objStr);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } catch (ClassNotFoundException cfe) {
                Log.i("SharedPreferencesUtil", "读取base64码对象时出现异常");
                cfe.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 获取UserDto
     */
    public static UserDto readUserDto() {
        return read(Constant.SharedPreferences.GLOBAL_SP ,
            Constant.SharedPreferences.USER_INFO, UserDto.class, null);
    }



    /**
     * 写入UserDto
     */
    public static void writeUserDto(UserDto userDto) {
        write(Constant.SharedPreferences.GLOBAL_SP ,
                Constant.SharedPreferences.USER_INFO, userDto);
    }


    public static void writeUserNameAndPsd(String userName, String psd) {
        write(Constant.SharedPreferences.GLOBAL_SP , Constant.SharedPreferences.USER_NAME,
            userName);
        write(Constant.SharedPreferences.GLOBAL_SP , Constant.SharedPreferences.USER_PWD,
            psd);
    }

    public static String[] readUserNameAndPsd() {
        String[] userNameAndPsd = new String[2];
        userNameAndPsd[0] = read(Constant.SharedPreferences.GLOBAL_SP ,
                Constant.SharedPreferences.USER_NAME, String.class,
            "");
        userNameAndPsd[1] = read(Constant.SharedPreferences.GLOBAL_SP,
                Constant.SharedPreferences.USER_PWD, String.class,
            "");
        return userNameAndPsd;
    }


}
